{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "87d98f54",
   "metadata": {},
   "source": [
    "# Week 6 Optional Extra - Deep Neural Network\n",
    "\n",
    "Just to redeem ourselves from the disappointing result\n",
    "\n",
    "This is very optional to run yourself! Switch to the other notebook redemption_run to load the trained file and run it!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "16d36f7e",
   "metadata": {},
   "outputs": [],
   "source": [
    "from dotenv import load_dotenv\n",
    "import os\n",
    "from huggingface_hub import login\n",
    "from pricer.evaluator import evaluate\n",
    "from pricer.deep_neural_network import DeepNeuralNetworkRunner\n",
    "from pricer.items import Item"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "61b42fbf",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Note: Environment variable`HF_TOKEN` is set and is the current active token independently from the token you've just configured.\n"
     ]
    }
   ],
   "source": [
    "# environment\n",
    "\n",
    "LITE_MODE = False\n",
    "\n",
    "load_dotenv(override=True)\n",
    "hf_token = os.environ['HF_TOKEN']\n",
    "login(hf_token, add_to_git_credential=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "ac3f0efc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Loaded 800,000 training items, 10,000 validation items, 10,000 test items\n"
     ]
    }
   ],
   "source": [
    "username = \"ed-donner\"\n",
    "dataset = f\"{username}/items_lite\" if LITE_MODE else f\"{username}/items_full\"\n",
    "\n",
    "train, val, test = Item.from_hub(dataset)\n",
    "\n",
    "print(f\"Loaded {len(train):,} training items, {len(val):,} validation items, {len(test):,} test items\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "abef5881",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Deep Neural Network created with 289,128,449 parameters\n",
      "Using mps\n"
     ]
    }
   ],
   "source": [
    "runner = DeepNeuralNetworkRunner(train, val[:1000])\n",
    "runner.setup()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "43155e26",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "37817f4ddac7474398916b02dc3c6f9e",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "  0%|          | 0/12500 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Epoch [1/5]\n",
      "Train Loss: 0.5463, Val Loss: 0.4324\n",
      "Val mean absolute error: $58.26\n",
      "Learning rate: 0.001000\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "93f4ab1f0cb04088a96184e7f10df984",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "  0%|          | 0/12500 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Epoch [2/5]\n",
      "Train Loss: 0.3823, Val Loss: 0.4100\n",
      "Val mean absolute error: $56.25\n",
      "Learning rate: 0.000976\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "11beaa8cb4b84e0497ab724452c4cd50",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "  0%|          | 0/12500 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Epoch [3/5]\n",
      "Train Loss: 0.3230, Val Loss: 0.4090\n",
      "Val mean absolute error: $56.55\n",
      "Learning rate: 0.000905\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "1d217af0f0324ac5b465ccc877782a0e",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "  0%|          | 0/12500 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Epoch [4/5]\n",
      "Train Loss: 0.2806, Val Loss: 0.4037\n",
      "Val mean absolute error: $55.70\n",
      "Learning rate: 0.000794\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "eb21815638df485c88da5081bee47bc0",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "  0%|          | 0/12500 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Epoch [5/5]\n",
      "Train Loss: 0.2446, Val Loss: 0.3944\n",
      "Val mean absolute error: $53.15\n",
      "Learning rate: 0.000655\n"
     ]
    }
   ],
   "source": [
    "runner.train(epochs=5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e0543d21",
   "metadata": {},
   "outputs": [],
   "source": [
    "def deep_neural_network(item):\n",
    "    return runner.inference(item)\n",
    "\n",
    "evaluate(deep_neural_network, test)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "e14c0512",
   "metadata": {},
   "outputs": [],
   "source": [
    "runner.save('deep_neural_network.pth')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f84024f2",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
